package study.doc.word.support;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.junit.Test;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class LatexUtils {

    /* 正则表达式 */
    private static final Pattern PATTERN = Pattern.compile("\\$(.*?)\\$");

    /**
     * 线程安全的方法，抽取其间文案
     */
    public static List<String> match(String txt) {
        Matcher matcher = PATTERN.matcher(txt);
        List<String> resLt = Lists.newArrayList();
        while (matcher.find()) {
            resLt.add(matcher.group(1));
            matcher.replaceAll("123123123");
        }
        log.info(txt);
        return resLt;
    }

    public String test(String content) {
        Matcher matcher = PATTERN.matcher(content);
        String str = "";
        while (matcher.find()) {
            //获取
            String src = matcher.group(1);
            log.info(">>>>>src= {}", src);
            //替换
            str = matcher.replaceFirst("** " + src + " **");

            //下一个
            matcher = PATTERN.matcher(str);
        }
        return str;
    }


    @Test
    public void test() {
        //  想要提取 ( 和 : 之间的 title 字段
        List<String> dataLt = match("函数$f\\\\left( x \\\\right)={{x}^{2}}$的定义域是$R$，$f\\\\left( -x \\\\right)={{\\\\left( -x \\\\right)}^{2}}={{x}^{2}}=f\\\\left( x \\\\right)$，故函数$f\\\\left( x \\\\right)={{x}^{2}}$是偶函数，故答案为B.<br> ");
        dataLt.forEach(e -> {
            log.info(e);
        });
    }

}
